// pages/userCourse/detail.js
import {
  request,
  pageQuery,
  saveFormId,
  toPage
} from '../../utils/util.js'
var WxParse = require('../../wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentId: null,
    courseDetail: {},
    tab_index: 0,
  },
  toPage: toPage,
  changeTap(e){
    let { index } = e.currentTarget.dataset
    this.setData({
      tab_index: index
    })
  },
  openLocation(e){
    let { lat, lng, store_name, address} = e.currentTarget.dataset.obj
    lat = Number(lat)
    lng = Number(lng)
    if (!lat || !lng || lat == NaN || lng == NaN) {
      wx.showToast({
        title: '未设置地址！',
        icon: 'none'
      })
      return
    }
    
    wx.openLocation({
      name: store_name,
      address: address,
      latitude: lat,
      longitude: lng,
      scale: 18
    })
  },
  
  //扫码签到  
  scan: function (e) {
    let that = this;
    let id = e.currentTarget.dataset.id;
    wx.scanCode({
      onlyFromCamera: true,
      success: function (res) {
        let teacher = JSON.parse(res.result);
        if (!teacher.teacher_id || !teacher.phone) {
          wx.showToast({
            title: '无效的签到码',
            icon: 'none',
          })
          return;
        }
        that.sign({
          id: id,
          teacher_id: teacher.teacher_id,
          phone: teacher.phone,
        });
      },
      fail: function (res) {
        wx.showToast({
          title: '无效的签到码',
          icon: 'none',
        })
      }
    })
  },

  //签到
  sign: function (data) {
    let that = this;
    request({
      url: 'User/scanCodeSign',
      method: 'POST',
      data: data,
      success: data => {
        wx.showModal({
          title: '温馨提示',
          content: '签到成功',
          showCancel: false,
          success: function (res) {
            that.queryBookList(true)
          },
        })
      }
    })
  },

  cancel(e){
    let {id} = e.currentTarget.dataset
    wx.showModal({
      title: '确认要取消吗？',
      content: '',
      success: res => {
        let { confirm } = res
        if (confirm) {
          request({
            url: 'User/cancelBook',
            data: {
              id: id
            },
            success: res => {
              this.queryBookList(true)
            },
        
          })
        }
      }
    })
  },
  loadBookMore(){
    this.queryBookList()
  },
  queryBookList(reload = false) {
    pageQuery({
      url: 'User/courseDetailTime',
      method: 'POST',
      data: {
        user_course_id: this.data.currentId
      },
      success: res => {
        console.log(res)
      }
    }, 'book', this, reload)
  },
  queryDetail() {
    request({
      url: 'User/courseDetail',
      data: {
        user_course_id: this.data.currentId
      },
      method: 'POST',
      success: data => {
        this.setData({
          courseDetail: data
        })
        WxParse.wxParse('courseContent', 'html', data.content || '', this, 5);
      },
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let { id } = options
    this.data.currentId = id || null
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.queryDetail()
    this.queryBookList(true)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})